package net.cyue.nest.nestjava.common.util;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public final class JSONUtil {
  private static ObjectMapper OBJECT_MAPPER = new ObjectMapper();
  static {
    OBJECT_MAPPER.setVisibility(
      PropertyAccessor.ALL,
      JsonAutoDetect.Visibility.ANY
    );
  }

  public static String toJSONString(Object obj) {
    try {
      return JSONUtil.OBJECT_MAPPER.writeValueAsString(obj);
    } catch (JsonProcessingException e) {
      throw new RuntimeException(e);
    }
  }

  public static <T> T parseObject(String text, Class<T> clz) {
    try {
      return JSONUtil.OBJECT_MAPPER.readValue(text, clz);
    } catch (JsonProcessingException e) {
      throw new RuntimeException(e);
    }
  }
}
